//
//  WmcShareAlertView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/8/22.
//

import UIKit

// MARK: - [WmcWatermarkShareAlertView]

class WmcWatermarkShareAlertView: WmcAlertView {
    lazy var watermarkPreviewImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.backgroundColor = .wmc_grayH2_323233
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()

    var useWatermarkBlock: YPBlock.OneParam<WmcWatermarkDetailModel>?

    var watermarkModel: WmcWatermarkDetailModel?

    override func makeUI() {
        layer.cornerRadius = 8
        clipsToBounds = true

        addSubview(horizontalLine)
        addSubview(verticalLine)
        addSubview(cancelBtn)
        addSubview(confirmBtn)

        titleLabel.text = "你收到一个水印"
        titleLabel.textAlignment = .left
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(22)
        }

        contentLabel.text = "使用此水印可能会丢失你目前编辑的内容"
        contentLabel.textAlignment = .left
        addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
            make.left.right.equalTo(titleLabel)
            make.height.equalTo(24)
        }

        addSubview(watermarkPreviewImageView)
        watermarkPreviewImageView.snp.makeConstraints { make in
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(horizontalLine.snp.top)
        }

        horizontalLine.snp.remakeConstraints { make in
            make.bottom.equalTo(-50)
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }

        contentType = .label
        style = .custom(confirmTitle: "立即使用", cancelTitle: "取消")
    }

    override func bindViewAction() {
        cancelBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.cancelBlock?()
            weakSelf.dismiss()
        }

        confirmBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self,
                  let wmModel = weakSelf.watermarkModel else { return }
            weakSelf.useWatermarkBlock?(wmModel)
            weakSelf.dismiss()
        }
    }

    /// 配置模型
    func configModel(_ model: WmcMediaWatermarkShareTemplate) {
        watermarkModel = model.wmcInfo
        if let url = model.wmUrl.yp.url_optional {
            watermarkPreviewImageView.kf.setImage(with: url)
        }
    }
}

extension WmcWatermarkShareAlertView {
    /// 展示
    func show(completion: YPBlock.Empty? = nil) {
        show(height: 396, completion: completion)
    }
}
